﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Boom.Navigation
{
    public abstract class MultilFrame:DrawableGameComponent
    {
        protected Texture2D _Texture;
        protected SpriteBatch _SBatch;
        protected Vector2 _Position;
        protected Rectangle _Bounds;

        protected List<Rectangle> _FrameList;
        protected Rectangle _CurrentFrame;
        protected int _ActiveFrame;
        protected int _HoriFrame;

        protected int _Height;
        protected int _Width;

        public Texture2D Texture
        {
            get { return _Texture; }
            set { _Texture = value; }
        }

        public Vector2 Position
        {
            get { return _Position; }
            set { _Position = value; }
        }

        public Rectangle Bounds
        {
            get { return _Bounds; }
            set { _Bounds = value; }
        }

        public List<Rectangle> FrameList
        {
            get { return _FrameList; }
            set { _FrameList = value; }
        }

        public Rectangle CurrentFrame
        {
            get { return _CurrentFrame; }
            set { _CurrentFrame = value; }
        }

        public int ActiveFrame
        {
            get { return _ActiveFrame; }
            set { _ActiveFrame = value; }
        }

        public int Width
        {
            get { return _Width; }
            set { _Width = value; }
        }

        public int Height
        {
            get { return _Height; }
            set { _Height = value; }
        }

        public MultilFrame(Game Mygame, ref Texture2D Texture, int Width, int Height)
            : base(Mygame)
        {
            _SBatch = (SpriteBatch)Mygame.Services.GetService(typeof(SpriteBatch));
            this.Texture = Texture;
            Position = new Vector2();
            FrameList = new List<Rectangle>();
            _Width = Width;
            _Height = Height;

            int xFrame = this.Texture.Width / _Width;
            int yFrame = this.Texture.Height / _Height;
            int X = -Width, Y = -Height;

            for (int i = 0; i < yFrame; i++)
            {
                Y += Height;
                X = -Width;
                for (int j = 0; j < xFrame; j++)
                {
                    FrameList.Add(new Rectangle(X += Width, Y, _Width, _Height));
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            CurrentFrame = FrameList[ActiveFrame];
            if (Enabled)
                _SBatch.Draw(Texture, Position + Config.Margin, CurrentFrame, Color.White);
          
            base.Draw(gameTime);
        }

    }
}
